﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Text;

public partial class AppendSortBook : System.Web.UI.Page
{
    private DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
       
            DoBind();
        
    }

    private void DoBind()
    {
        try
        {
            string filename = HttpUtility.HtmlDecode(Request.QueryString["filename"]);

            ds = new DataSet();
            OpenFile(ref ds, filename);
            DataTable dt = new DataTable();
            TableRow newrow = new TableRow();
            int irowcount = ds.Tables[0].Rows.Count;
            int icolcount = ds.Tables[0].Columns.Count;

            TableCell cell = new TableCell();
            cell.Text = ds.Tables[0].Columns[0].ToString();
            newrow.Cells.Add(cell);
            cell = new TableCell();
            cell.Text = ds.Tables[0].Columns[1].ToString();
            newrow.Cells.Add(cell);
            cell = new TableCell();
            cell.Text = ds.Tables[0].Columns[2].ToString();
            newrow.Cells.Add(cell);
            cell = new TableCell();
            cell.Text = "排序";
            newrow.Cells.Add(cell);
            Table1.Rows.Add(newrow);

            for (int i = 0; i < irowcount; i++)
            {
                DropDownList ddl = new DropDownList();
                for (int j = 0; j <= irowcount; j++)
                {
                    ddl.Items.Add(j.ToString());
                }
                ddl.SelectedIndex = i + 1;
                TableRow row = new TableRow();
                cell = new TableCell();
                cell.Text = ds.Tables[0].Rows[i][0].ToString();
                row.Cells.Add(cell);
                cell = new TableCell();
                cell.Text = ds.Tables[0].Rows[i][1].ToString();
                row.Cells.Add(cell);
                cell = new TableCell();
                cell.Text = ds.Tables[0].Rows[i][2].ToString();
                row.Cells.Add(cell);
                cell = new TableCell();
                cell.Controls.Add(ddl);
                row.Cells.Add(cell);
                Table1.Rows.Add(row);
            }   
        }
        catch (Exception ee)
        {
            
            throw ee;
        }
       
    }

    private void OpenFile(ref DataSet ds,string filename)
    {
        string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +Server.MapPath("Files")+"\\" + filename + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
        OleDbConnection conn = new OleDbConnection(strConn);
        conn.Open();
        string strExcel = "";
        OleDbDataAdapter myCommand = null;
        strExcel = "select * from [Sheet1$A4:"+ HttpUtility.UrlDecode(Request.QueryString["endlocation"]) +"]";
        myCommand = new OleDbDataAdapter(strExcel, strConn);
        ds = new DataSet();

        myCommand.Fill(ds);
        conn.Close();

    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        string filename = HttpUtility.HtmlDecode(Request.QueryString["filename"]);
        FileInfo file = new FileInfo(Server.MapPath("Files") + "\\" + filename);
        file.Delete();
        string js = @"<Script language='JavaScript'>
                    window.close();  
                  </Script>";
        HttpContext.Current.Response.Write(js);
        HttpContext.Current.Response.End();        
      
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Items");
        dt.Columns.Add("Declare");
        dt.Columns.Add("Note");
        dt.Columns.Add("SortList");
        dt.Columns[3].DataType = System.Type.GetType("System.Int32");
        for (int i = 1; i < Table1.Rows.Count; i++)
        {
            DataRow row = dt.NewRow();
            row[0] = Table1.Rows[i].Cells[0].Text;
            row[1] = Table1.Rows[i].Cells[1].Text;
            row[2] = Table1.Rows[i].Cells[2].Text;
            row[3] = Convert.ToInt32(((DropDownList)Table1.Rows[i].Cells[3].Controls[0]).SelectedItem.Text);
            
            dt.Rows.Add(row);
        }
        DataView dv = new DataView(dt);
        dv.Sort = "SortList asc";
        dt = dv.ToTable();
        Session["dt"] = dt;
        string js = @"<Script language='JavaScript'>
                    window.close();  
                  </Script>";
        HttpContext.Current.Response.Write(js);
        HttpContext.Current.Response.End();        
    }
}